﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InTheHand.Net.Sockets;
using Newtonsoft.Json.Linq;
using WinHBM.Core.Bluetooth;

namespace WinHBM.Core.Net.Sockets.Cmd
{
    public class BluetoothConnectCmd : SocketCmd
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(BluetoothConnectCmd));

        private BluetoothManager bluetoothManager;
        private FlashSocketServer socket;

        public override void executeCmd(JObject request, FlashSocketServer socket)
        {
            string address = request["address"].ToString(); 
            this.socket = socket;
            try
            {
                bluetoothManager = new BluetoothManager();
                BluetoothClient client = bluetoothManager.Connect(address);
                BluetoothClientHolder.bluetoothClient = client;
                BluetoothClientHolder.magicBar = new MagicBar(client);
                BluetoothClientHolder.magicBar.CMD_GetNowTime();
                BluetoothClientHolder.Address = address;
                socket.Send(wrapCmd(CMD_BLUETOOTH_CONNECT, new JObject()));
            }
            catch (Exception e)
            {
                Logger.Error(e);
                socket.Send(wrapCmd(CMD_BLUETOOTH_CONNECT,"连接失败"));
                
            }
           

        }

        private void BluetoothManager_DiscoveryCompleted()
        {

        }

        private void BluetoothManager_FoundDevice(BluetoothManager manager, InTheHand.Net.Sockets.BluetoothDeviceInfo device)
        {

        }
    }
}
